<?php

Yii::import('application.models._base.BaseNews');

class News extends BaseNews
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	public static function getArticleByCat($catId, $limit=null, $where='',$order='')
	{
		$limit = ($limit!=null)?$limit:5;
		$order = ($order!='')?$order:'ORDER BY id DESC';
		$sql = "SELECT tc.title, tc.id, tc.images, tc.introtext, tc.alias, tc.catid
				FROM tbl_content as tc
				WHERE tc.published = 1 AND tc.catid= $catId AND tc.type='post'
				$order
				LIMIT $limit
				OFFSET 0
		";
		return Yii::app()->db->createCommand($sql)->queryAll();
	}
	public static function getArticleByCatAlias($cat_alias, $limit=null, $where='',$order='')
	{
		$limit = ($limit!=null)?$limit:5;
		$order = ($order!='')?$order:'ORDER BY id DESC';
		$sql = "SELECT tc.title, tc.id, tc.images, tc.introtext, tc.alias, tc.catid
				FROM tbl_content as tc
				LEFT JOIN  tbl_categories as cc ON tc.catid = cc.id
				WHERE tc.published = 1 AND tc.type='post' AND cc.alias=:p
				$order
				LIMIT $limit
				OFFSET 0
		";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':p', $cat_alias);
		return $command->queryAll();
	}
	public static function getArticleByAlias($alias)
	{
		$sql = "SELECT * 
				FROM tbl_content
				WHERE alias = :p AND type='post' AND published=1
				";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':p',$alias,PDO::PARAM_STR);
		return $command->queryRow();
	}
	public static function getIdByAlias($alias)
	{
		$sql = "SELECT id FROM tbl_content WHERE alias=:a";
		$command=Yii::app()->db->createCommand($sql);
		$command->bindParam(':a', $alias);
		return $command->queryScalar();
	}
}